package org.chengyi.model_shop.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.chengyi.model_shop.entity.Product;
import org.chengyi.model_shop.mapper.ProductMapper;
import org.chengyi.model_shop.mapper.OrderMapper;
import org.chengyi.model_shop.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private OrderMapper orderMapper;
    @Override
    public PageInfo<Product> getProductList(int pageNum, int pageSize, Integer categoryId) {
        PageHelper.startPage(pageNum, pageSize);
        List<Product> products = productMapper.getProductListByCategory(categoryId);
        return new PageInfo<>(products);
    }

    @Override
    public Map<String, Object> addProduct(Product product) {
        Map<String, Object> result = new HashMap<>();
        // 业务校验
        if (product.getName() == null || product.getName().trim().isEmpty()) {
            result.put("success", false);
            result.put("message", "商品名称不能为空");
            return result;
        }
        if (product.getPrice() == null || product.getPrice() < 0) {
            result.put("success", false);
            result.put("message", "价格不合法");
            return result;
        }
        if (product.getStock() < 0) {
            result.put("success", false);
            result.put("message", "库存不合法");
            return result;
        }
        // 允许 imageUrl 为空
        if (product.getCreatedAt() == null) {
            product.setCreatedAt(new Date());
        }
        int rows = productMapper.addProduct(product);
        if (rows > 0) {
            result.put("success", true);
            result.put("message", "商品添加成功");
        } else {
            result.put("success", false);
            result.put("message", "商品添加失败");
        }
        return result;
    }

    @Override
    public List<Product> getByName(String name) {
        if (name == null || name.trim().isEmpty()) {
            return  null;
        }
        return productMapper.getByName(name);
    }

    @Override
    public String delete(Integer id) {
        // 检查order_item表是否有关联该商品的订单
        int count = orderMapper.countOrderItemByProductId(id);
        if (count > 0) {
            return "该商品有关联订单，无法删除";
        }
        return productMapper.delete(id) > 0 ? "删除成功" : "删除失败";
    }

    @Override
    public Product getById(Integer id) {
        if (id == null) {
            return null;
        }
        return productMapper.getById(id);
    }

    @Override
    public Map<String, Object> update(Product product) {
        Map<String, Object> result = new HashMap<>();
        if (product.getName() == null || product.getName().trim().isEmpty()) {
            result.put("success", false);
            result.put("message", "商品名称不能为空");
            return result;
        }
        if (product.getPrice() == null || product.getPrice() < 0) {
            result.put("success", false);
            result.put("message", "价格不合法");
            return result;
        }
        if (product.getStock() < 0) {
            result.put("success", false);
            result.put("message", "库存不合法");
            return result;
        }
        int rows = productMapper.update(product);
        if (rows > 0) {
            result.put("success", true);
            result.put("message", "商品更新成功");
        } else {
            result.put("success", false);
            result.put("message", "商品更新失败");
        }
        return result;
    }

    @Override
    public String getDescriptionById(Integer id) {
        return productMapper.getDescriptionById(id);
    }
}
